﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using Business.Classes;
using System.Collections.Generic;

namespace UI
{
    public partial class _Default : System.Web.UI.Page
    {
        Usuario user = null;
        
        PostNewBusiness postNewBusiness = new PostNewBusiness();
        UsuarioBusiness usuarioBusiness = new UsuarioBusiness();
        PropagandaBusiness propagandaBusiness = new PropagandaBusiness();

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (Usuario)Session["user"];

            if (!IsPostBack)
            {
                //Grid de PostNews
                gvPostNew.DataSource = Session["pn"] = (from d in postNewBusiness.GetTopPostNews(10)
                                                        select new
                                                        {
                                                            d.idPostNew,
                                                            d.titulo,
                                                            d.Imagem.idImagem,
                                                            d.data
                                                        }).ToList();
                gvPostNew.DataBind();
                ///Carregando imagens da post new no gridvew
                for (int i = 0; i < gvPostNew.Rows.Count; i++)
                {
                    Image imgPostNew = (Image)gvPostNew.Rows[i].FindControl("imgPostNew");
                    Label lbIDImagem = (Label)gvPostNew.Rows[i].FindControl("lbIDImagem");
                    imgPostNew.ImageUrl = "~/FrmViewImagePostNew.aspx?idImagem=" +lbIDImagem.Text;
                }

                //Grid de Usuarios
                dvUsuarios.DataSource = usuarioBusiness.getUsersWithMorePoints(5);
                dvUsuarios.DataBind();
                //Grid de patrocinadores
                dvPatrocinadores.DataSource = propagandaBusiness.getAllPropaganda();
                dvPatrocinadores.DataBind();

                ///Carregando imagens dos Patrocinadores
                for (int i = 0; i < dvPatrocinadores.Rows.Count; i++)
                {
                    Image imgPostNew = (Image)dvPatrocinadores.Rows[i].FindControl("imgPostNew");
                    Label lbIDImagem = (Label)dvPatrocinadores.Rows[i].FindControl("lbIDImagem");
                    imgPostNew.ImageUrl = "~/FrmViewImagePostNew.aspx?idImagem=" + lbIDImagem.Text;
                }

            }
        }

        protected void gvPostNew_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            
            //Image ImagePostNew = (Image)gvPostNew.FindControl("gvPostNew");
            //ImagePostNew. = "~/FrmViewImagePostNew.aspx?idImagem=" + postNew.Imagem.idImagem.ToString();
        }

        protected void gvPostNew_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPostNew.PageIndex = e.NewPageIndex;
            gvPostNew.DataSource = Session["pn"];
            gvPostNew.DataBind();
        }
    }
}
